 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.activity
 * File     : ProviderList.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2011-11-8
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.activity;

import net.solosky.easyavatar.R;
import net.solosky.easyavatar.bean.AuthInfo;
import net.solosky.easyavatar.bean.ProviderMeta;
import net.solosky.easyavatar.bean.ProviderMeta.AuthType;
import net.solosky.easyavatar.utils.Globals;
import net.solosky.easyavatar.utils.Views;
import net.solosky.easyavatar.view.ListOptionDialog;
import net.solosky.easyavatar.view.ListOptionDialog.ListOption;
import net.solosky.easyavatar.view.ListOptionDialog.OnListOptionClickListener;
import net.solosky.easyavatar.view.ProviderListAdapter;
import net.solosky.easyavatar.view.SimpleListAdapter;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

/**
 *
 * 显示所有可用的头像接口
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class Providers extends Base implements OnItemClickListener, OnItemLongClickListener, OnListOptionClickListener{
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(!checkInitilized()){ finish(); return; }
        
        setContentView(R.layout.ui_providers);
        
        setTitle(R.string.providers);
        setupBackButton();
        
        ListView providersView = (ListView) this.findViewById(R.id.lv_providers);
        providersView.setAdapter(new ProviderListAdapter(this, Globals.PROVIDER_META_LIST));
        providersView.setOnItemClickListener(this);
        providersView.setOnItemLongClickListener(this);
    }
	


	@Override
	protected void onResume() {
		super.onResume();
		redrawProvidersListView();
	}

	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		ProviderMeta meta = Globals.PROVIDER_META_LIST.get(position);
		AuthInfo authInfo = meta.getAuthInfo();
		if(authInfo!=null && 
				meta.getAuthType()==AuthType.OAUTH && 
				authInfo.isAuthorized()){
			startViewFriend(meta);
		}else{
			startLoginOrAuthronize(meta);
		}
	}


	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		ProviderMeta meta = (ProviderMeta) parent.getAdapter().getItem(position);
		AuthInfo auth = meta.getAuthInfo();
		String item = null;
		if(auth!=null){
			if(meta.getAuthType()==AuthType.OAUTH && auth.isAuthorized()){
				item = Views.S(R.string.opt_cencel_authronize);
			}else if(meta.getAuthType()==AuthType.WEB && auth.isAutoLogin()){
				item = Views.S(R.string.opt_cancel_auto_login);
			}else{
				item = Views.S(meta.getAuthType()==AuthType.OAUTH ? R.string.authronize : R.string.login);
			}
		}else{
			item = Views.S(meta.getAuthType()==AuthType.OAUTH ? R.string.authronize : R.string.login);
		}
		
		new ListOptionDialog(this, (ListView) parent, position, this)
		.addListOption(0, item)
		.setTitle(meta.getTitle())
		.show();
		return false;
	}


	private void startLoginOrAuthronize(ProviderMeta meta){
		Intent intent = new Intent(this, Login.class);
		intent.putExtra("provider.from", "providers");
		intent.putExtra("provider.code", meta.getCode());
		startActivity(intent);
	}
	
	
	private void startViewFriend(ProviderMeta meta){
		Intent intent = new Intent(this, Friends.class);
		intent.putExtra("provider.code", meta.getCode());
		startActivity(intent);
	}
	
	private void redrawProvidersListView(){
		 ListView lv = (ListView) findViewById(R.id.lv_providers);
		 SimpleListAdapter ad = (SimpleListAdapter) lv.getAdapter();
		 ad.notifyDataSetChanged();
	}

	@Override
	public void onClick(ListOptionDialog dialog, ListOption option) {
		ProviderMeta meta = (ProviderMeta) dialog.getItemData();
		AuthInfo auth = meta.getAuthInfo();
		if(auth!=null){
			if(meta.getAuthType()==AuthType.OAUTH && auth.isAuthorized()){
				auth.setAuthorized(false);
				auth.setToken("");
				auth.setSecret("");
				Globals.DB_PERSIST.saveAuthInfo(auth);
				redrawProvidersListView();
			}else if(meta.getAuthType()==AuthType.WEB && auth.isAutoLogin()){
				auth.setAutoLogin(false);
				auth.setPassword("");
				Globals.DB_PERSIST.saveAuthInfo(auth);
				redrawProvidersListView();
			}else{
				startLoginOrAuthronize(meta);
			}
		}else{
			startLoginOrAuthronize(meta);
		}
	}
}
